package controllers.listeners.semrep;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

import controllers.MainModuleController;

import objects.Concept;
import objects.Constituent;
import views.MainPanel;

/* Edits the highlighted semantic's concept and sense */
/* actionPerformed when you click on the "Edit Constituent" button */
public class EditConstituentActionListener implements ActionListener {
	
	MainPanel view;
	
	public EditConstituentActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		Constituent currCon = view.getCurrCon();
		
		if (currCon != null) {
			JTextField txtConcept = new JTextField();
			JTextField txtSense = new JTextField();
			
			Object[] message = { "Enter concept value:",
					txtConcept, "Enter sense value:",
					txtSense
			};
			String title = null;
			
			if (currCon.getConcept() == null || currCon.getConcept().getName() == null || currCon.getConcept().getName().equals("")) {
				title = "";
			}
			else {
				title = " \"" + currCon.getConcept().getName() + "-" + currCon.getConcept().getSense() + "\"";
			}
			title = ("Edit " + currCon.getLabel()).concat(title);
			
			txtConcept.setText(currCon.getConcept().getName());
			txtSense.setText(currCon.getSense());
			
			int option = JOptionPane.showConfirmDialog(null, message, title, JOptionPane.OK_CANCEL_OPTION);
			if (option == JOptionPane.OK_OPTION
					&& !txtConcept.equals("")
					&& txtConcept != null
					&& !txtSense.equals("")
					&& txtSense != null) {
				Concept concept = new Concept();
				concept.setName(txtConcept.getText());
				currCon.setConcept(concept);
				currCon.setSense(txtSense.getText());
			}
			
			view.clearPanels();
			view.loadSemantic(MainModuleController.setTextAreaSemantic(view.getConstituent(), view));
		}
	}
	
	public void setView(MainPanel view) {
		this.view = view;
	}
}
